# -*- coding: utf-8 -*-
# @Time: 2023/4/24 0:52
# @File: decrorators.py
# @Desc:

from flask import g
from functools import wraps
from utils import restful


def permission_required(permission):
    def outter(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            user = getattr(g, "user")
            if not user:
                return restful.unlogin_error()
            if user.check_permission(permission):
                return func(*args, **kwargs)
            else:
                return restful.params_error(message="不被允许的请求！")
        return wrapper
    return outter
